package com.example

import java.util.Scanner

import scala.util.Random
import scala.collection.mutable.Map

/**
 * 人机猜拳
 *
 * @author lbq
 * @date 2022/1/6 22:34
 * @version 1.0
 */
object HomeWork2 {
  def main(args: Array[String]): Unit = {
    val role = Array("刘备", "关羽", "张飞")
    val skill = Array("剪刀", "石头", "布")
    print("请选择对战角色:(")
    for (i <- role.indices) print(s"${i + 1}.${role(i)}  ")
    println(")")
    val scanner: Scanner = new Scanner(System.in)
    var roleId = scanner.nextInt()
    while (roleId < 1 || roleId > role.length) {
      println("请输入1 -> 3之间的整数...")
      roleId = scanner.nextInt()
    }
    println(s"你选择了与${role(roleId - 1)}对战")
    println("要开始么? y/n")
    var str = scanner.next()

    // 游客的结果
    var result1 = Map("胜局" -> 0, "和局" -> 0, "负局" -> 0)
    // 电脑的结果
    var result2 = Map("胜局" -> 0, "和局" -> 0, "负局" -> 0)
    var num = 0

    while (str.equals("y")) {
      num += 1
      print("请出拳! ")
      for (i <- skill.indices) print(s"${i + 1}.${skill(i)}  ")
      println("")
      var i = scanner.nextInt()
      if (i < 1 || i > 3) {
        println("输入不符合规范,默认出布!")
        i = 3
      } else {
        println(s"你出拳:${skill(i - 1)}")
      }
      println(s"${role(roleId - 1)}出拳!")
      val random = Random.self
      val j = random.nextInt(skill.length)
      println(s"${role(roleId - 1)}出拳:${skill(j)}")
      if (i - (j + 1) == 1 || i - (j + 1) == -2) {
        result1("胜局") = result1("胜局") + 1
        result2("负局") = result2("负局") + 1
        println("结果: 恭喜,你赢啦!")
      } else if (i - (j + 1) == -1 || i - (j + 1) == 2) {
        result1("负局") = result1("负局") + 1
        result2("胜局") = result2("胜局") + 1
        println(s"结果: ${role(roleId - 1)}赢!")
      } else if (i == j + 1) {
        result1("和局") = result1("和局") + 1
        result2("和局") = result2("和局") + 1
        println("结果: 和局!下次继续努力!")
      }
      println("是否开始下一轮? y/n")
      str = scanner.next()
    }
    println("退出游戏!")
    println("--------------------------------")
    println(s"${role(roleId - 1)}  VS  游客")
    println(s"对战次数${num}次")
    println("")
    println("姓名\t等分\t胜局\t和局\t负局\t")
    println(s"游客\t${result1("胜局") * 2 + result1("和局")}\t${result1("胜局")}\t${result1("和局")}\t${result1("负局")}\t")
    println(s"${role(roleId - 1)}\t${result2("胜局") * 2 + result2("和局")}\t${result2("胜局")}\t${result2("和局")}\t${result2("负局")}\t")
  }
}
